<template>
  <el-dialog
    v-el-drag-dialog
    title="退单"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" label-position="right" label-width="80px" style="margin: 0 50px;">
      <el-form-item label="客户名称:">
        <el-input v-model="temp.csmName" disabled />
      </el-form-item>
      <el-form-item label="客户电话:">
        <el-input v-model="temp.csmPhone" disabled />
      </el-form-item>
      <el-form-item class="block" label="退单理由:" prop="remark">
        <el-checkbox-group v-if="isExist === 1" v-model="checkedTypes">
          <el-checkbox v-for="item in commonKeysInfoList" :key="item.keyId" :value="item.keyId" :label="item.keyName" />
        </el-checkbox-group>
        <el-input v-model="temp.remark" type="textarea" :rows="3" :maxlength="isExist === 1 ? 250 : 100" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryOrderBack } from '@/api'

  export default {
    name: 'back-order',
    data() {
      return {
        visible: false,
        isExist: 1,
        commonKeysInfoList: [], // 退单理由复选框
        checkedTypes: [],
        orderCode: '',
        csmName: '',
        csmPhone: '',
        temp: {
          remark: ''
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, JSON.parse(temp.billInfo), {
              remark: ''
            })
            this.isExist = Number(temp.isExist)
            this.commonKeysInfoList = JSON.parse(temp.commonKeysInfoList)
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            orderCode: this.temp.orderCode,
            ownerId: this.temp.ownerId || 0
          }
          if (this.isExist === 0) {
            params.remark = this.temp.remark
          } else {
            let reason = ''
            if (this.checkedTypes.length > 0) {
              this.checkedTypes.forEach(item => {
                reason += `${item}，`
              })
            }
            const reason2 = this.temp.remark
            if (reason !== '' && reason2 === '') {
              params.remark = reason.substring(0, reason.length - 1)
            } else {
              params.remark = reason + reason2
            }
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryOrderBack(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
